﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LockCrypt.Mobile {
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Drawing.Imaging;

    public class ImageButton : Control {
        /// <summary>
        /// A button which displays an image
        /// Shamelessly ripped from MSDN - http://msdn.microsoft.com/en-us/library/aa446518.aspx
        /// </summary>
        private Image image;
        private bool isPushed;
        private Bitmap bmpOffscreen; // Double buffering bitmap

        /// <summary>
        /// Gets or sets the image to display.
        /// </summary>
        /// <value>The image.</value>
        public Image Image {
            get { return image; }
            set { image = value; }
        }

        public ImageButton() {
            isPushed = false;
            this.Size = new Size(20, 20);
        }
        public ImageButton(Image image)
            : this() {
            this.Image = image;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {
            Graphics gxOff;      //Offscreen graphics
            Rectangle imgRect; //image rectangle
            Brush backBrush;   //brush for filling a backcolor

            if(bmpOffscreen == null) {
                bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
            }

            gxOff = Graphics.FromImage(bmpOffscreen);

            gxOff.Clear(this.BackColor);

            if(!isPushed) {
                backBrush = new SolidBrush(Parent.BackColor);
            } else {
                backBrush = new SolidBrush(Color.LightGray);
            }

            gxOff.FillRectangle(backBrush, this.ClientRectangle);

            if(image != null) {
                int imageLeft = (this.Width - image.Width) / 2;
                int imageTop = (this.Height - image.Height) / 2;
                if(!isPushed) {
                    imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
                } else {
                    //Shift the image by one pixel to make it look pushed
                    imgRect = new Rectangle(imageLeft + 1, imageTop + 1, image.Width,image.Height);
                }
                //Set transparent key
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image));
                gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height,GraphicsUnit.Pixel, imageAttr);

            }

            if(isPushed) {
                Rectangle rc = this.ClientRectangle;
                rc.Width--;
                rc.Height--;
                gxOff.DrawRectangle(new Pen(Color.Black), rc);
            }
            e.Graphics.DrawImage(bmpOffscreen, 0, 0);
            base.OnPaint(e);
        }
        private Color BackgroundImageColor(Image image) {
            Bitmap bmp = new Bitmap(image);
            return bmp.GetPixel(0, 0);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) {
            isPushed = true;
            this.Invalidate();
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) {
            isPushed = false;
            this.Invalidate();
        }
        protected override void OnPaintBackground(PaintEventArgs e) {
            //Do nothing
        }

    }
}
